import re

def sort_changelog(input_file, output_file):
    # 读取输入文件
    with open(input_file, 'r', encoding='utf-8') as f:
        lines = f.readlines()
    
    # 提取页码并排序
    entries = []
    for line in lines:
        line = line.strip()
        if not line:
            continue
        # 使用正则表达式匹配页码
        match = re.match(r'- P(\d+):', line)
        if not match:
            continue
        page_num = int(match.group(1))
        entries.append((page_num, line))
    
    # 按照页码排序
    entries.sort(key=lambda x: x[0])
    
    # 写入输出文件
    with open(output_file, 'w', encoding='utf-8') as f:
        for _, line in entries:
            f.write(line + '\n')

# 调用函数处理文件
input_file = './others/changelog_part2.md'
output_file = 'sorted_changelog.md'
sort_changelog(input_file, output_file)
